<template>
	<view>
		<!-- 标题 -->
		<view class="navbar">
			<u-icon @click="navigateBack" class="icon-left" name="arrow-left" color="#333" size="50"></u-icon>
			<text @click='toMuti'>绑定设备</text>
		</view>
		<!-- 内容 -->
		<view class="section">
			<image src="https://srmzln.oss-cn-hangzhou.aliyuncs.com/znl/images/binding.png" mode=""></image>
		</view>
		<view class="binding">
			<view @click="invitation" style="border: rgb(68,92,223) 1px solid;color: rgb(68,92,223);">
				<text>已收到管理员绑定邀请</text>
			</view>
			<view style="background-color: rgb(68,92,223);color: #fff;" @click="scanCode"><text>扫描绑定</text></view>
			<text @click="guide">绑定指南</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: '',
				deviceInfo:''
			}
		},
		mounted() {
			this.getInviteList()
		},
		onLoad(query) {

			const scene = decodeURIComponent(query.scene);
			//类型为string，所以只能判断不叫"undefined"
			//console.log(typeof(scene));
			if (scene != "undefined" && scene.length > 4) {
				console.log(scene);
				uni.showModal({
					content: '证书编码:' + scene + '，点击查询查看详情',
					showCancel: false
				});
				this.code = scene;
				bindDevice();
			} else {
				console.log("没有启动参数");

			}
		},
		methods: {
			toMuti(){
					uni.navigateTo({
						url:'./bindingMuti/bindingMuti'
					})
			},
			async getInviteList() {
				let res = await this.$api.inviteList()
				let list = []
				if (res.code === 200) {
					list = res.result.list
					if (list.length) {
						list.forEach(ele => {
							if (ele.deviceCode) {
								this.code = ele.deviceCode
								this.deviceInfo = ele
							}
						})
					}

				}
			},
			navigateBack() {
				// wx.reLaunch({
				// 	url: '../login'
				// })
				let pages = getCurrentPages();
				let nowPage = pages[pages.length - 1]
				let prevPage = pages[pages.length - 2]
				
				uni.navigateBack({
					delta:prevPage
				})
			},
			guide() {
				wx.reLaunch({
					url: './bindingGuide'
				})
			},
			invitation() {
				// wx.reLaunch({
				// 	url: './invitation'
				// })
				if (this.code) {
					uni.navigateTo({
						url: '/pages/login/binding/device/device?code=' + this.code
					})
				} else {
					uni.showToast({
						title: '暂无人邀请过你！'
					})
				}
			},
			scanCode() {
				var _this = this;
				uni.scanCode({
					success: function(res) {
						_this.code = res.result;
						console.log(res.result,9897)
						_this.code = res.result.substring(29)
						_this.bindDevice();
					},
					fail: function(res) {

						uni.showModal({
							title: '设备扫码',
							content: res.message || '设备扫描失败'
						});
					}
				})
			},
			async bindDevice() {

				uni.navigateTo({
					url: '/pages/login/binding/device/device?code=' + this.code
				})
				// 	let tpl_param = {
				// 		code: this.code
				// 	};
				// 	uni.showLoading({
				// 		title: '加载中',
				// 		mask: true
				// 	});
				// 	let res = await this.$api.bindDevice(tpl_param);
				// 	if (res.code == 200) {
				// 		uni.showToast({
				// 			title: '设备绑定成功'
				// 		});
				// 		setTimeout(() => {
				// 			uni.reLaunch({
				// 				url:'/pages/index/index'
				// 			})
				// 		}, 1000)
				// 	} else {
				// 		uni.showToast({
				// 			icon: 'error',
				// 			title: res.message || '设备绑定失败'
				// 		});
				// 	}
				// 	uni.hideLoading();
			}
		}
	}
</script>

<style scoped lang="scss">
	// 头部
	.navbar {
		margin-top: 90rpx;
		margin-bottom: 80rpx;
		width: 750rpx;
	}

	.navbar>text {
		font-size: 36rpx;
		font-weight: bold;
		padding-left: 220rpx;
	}

	.icon-left {
		margin-top: 20rpx;
		margin-left: 10rpx;
	}

	// 内容
	.section {
		padding: 100rpx;
	}

	.section>image {
		height: 320rpx;
		width: 550rpx;
	}

	// 按钮
	.binding {
		width: 750rpx;
		text-align: center;
		margin-top: 100rpx;
		padding: 30rpx;
		padding-bottom: 340rpx;
		font-size: 36rpx;
		color: rgb(68, 92, 223);
	}

	.binding>view {
		box-shadow: 1px 1px 2px rgb(228, 227, 227), 0 0 2px rgb(228, 227, 227), 0 0 1px rgb(228, 227, 227);
		border-radius: 50rpx;
		text-align: center;
		margin-bottom: 35rpx;
		padding: 25rpx;
	}
</style>
